<?php
namespace app\api\model;

class Coze1231 extends Base
{
    //扣子

    //获取会话
    public function hhOne($imei)
    {
        return $this->dbConn($this->dbHh)->where('imei', $imei)->find();
    }

    //创建会话
    public function hhAdd($hhid, $imei)
    {
        return $this->dbConn($this->dbHh)->insertGetId([
            'imei' => $imei, 'hh_id' => $hhid, 'addtime' => $this->toDate(),
        ]);
    }

    //创建对话
    public function runAdd($hhid, $chat_id)
    {
        $res = $this->dbConn($this->dbHhchat)
            ->where('hh_id', $hhid)
            ->where('chat_id', $chat_id)
            ->find();
        if (!$res) {
            $this->dbConn($this->dbHhchat)->insertGetId([
                'hh_id' => $hhid,'chat_id' => $chat_id,
                'addtime' => $this->toDate(),
            ]);
        }
        return $chat_id;
    }

    //用户消息添加
    public function msgUser($chat_id, $msg)
    {
        return $this->dbConn($this->dbHhmsg)->insertGetId([
            'hhc_id' => $chat_id, 'role' => 'user', 'cnt' => $msg,
            'addtime' => $this->toDate(),
        ]);
    }

    //智能体消息添加
    public function botUser($chat_id, $msg)
    {
        return $this->dbConn($this->dbHhmsg)->insertGetId([
            'hhc_id' => $chat_id, 'role' => 'assistant', 'cnt' => $msg,
            'addtime' => $this->toDate(),
        ]);
    }

    //讯飞语音转文字
    public function xfSet($chat_id, $cnt)
    {
        //讯飞文本
        $this->dbConn($this->dbHhRead)->insertGetId([
            'chat_id' => $chat_id, 'cnt' => $cnt, 'addtime' => $this->toDate(),
        ]);
        //对话信息
        $this->dbConn($this->dbHhmsg)->insertGetId([
            'hhc_id' => $chat_id, 'role' => 'user', 'cnt' => $cnt,
            'addtime' => $this->toDate(),
        ]);
        return true;
    }

    //讯飞获取文字
    public function xfGet()
    {
        $start = date('Y-m-d H:i:s', (time() - 5 * 60));
        return $this->dbConn($this->dbHhRead)
            ->where('cnt', 'like', '%开门%')
            ->where('addtime', '>=', $start)
            ->find();
    }

    //获取对话
    public function hhGet()
    {
        $res = $this->dbConn($this->dbHhmsg)
            ->order('id', 'asc')
            ->select()->toArray();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $start = date('Y-m-d H:i:s', (time() - 5 * 60)); //5分钟前
        $arys = [];
        foreach ($res as $v) {
            $is_door = 0;
            if (strpos($v['cnt'], '开门') && $v['addtime'] >= $start) {
                $is_door = 1;
            }
            $arys[] = [
                'id' => $v['id'],
                'role' => $v['role']=='user' ? 1 : 2,
                'uname' => $v['role']=='user' ? 'U'.time() : '小嘉',
                'cnt' => $v['cnt'],
                'is_door' => $is_door,
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //获取智能体问题
    public function queOne()
    {
        return $this->dbConn($this->dbHhRead)
            ->where('msg_id', 0)
            ->order('id', 'asc')
            ->find();
    }

    //获取智能体保存
    public function queSet($msgid, $chat_id, $msg)
    {
        $res = $this->dbConn($this->dbHhmsg)
            ->where('hhc_id', $chat_id)
            ->where('cnt', $msg)
            ->find();
        if ($res) {
            return $this->toback(-2, '已回复');
        }
        $lastid = $this->dbConn($this->dbHhmsg)->insertGetId([
            'hhc_id' => $chat_id, 'role' => 'assistant',
            'cnt' => $msg, 'addtime' => $this->toDate(),
        ]);
        $this->dbConn($this->dbHhRead)->where('id', $msgid)->update(['msg_id' => $lastid]);
        return $this->toback();;
    }
}